package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.model.Bank;
import com.model.BankDeposit;
import com.model.BankWithdraw;
import com.service.BankService;
import com.service.BranchService;

@Controller
public class BankController {

	@Autowired
	private BankService bankService;

	// REquest to add bank.. Give blank form of bank
	@RequestMapping(value="/user/addBank",method=RequestMethod.GET)
	public String addBank(@ModelAttribute("command") Bank bank) {
		return "bank";
	}

	// add bank details into database
	@RequestMapping(value = "/user/saveBank", method = RequestMethod.POST)
	public String saveBank(HttpSession session,@ModelAttribute("command") Bank bank) {
		bankService.add(session.getAttribute("branch").toString(),bank);
		return "redirect:/user/addBank.htm";
	}

	// give bank details from database
	@RequestMapping("/user/editBank")
	public ModelAndView editBank(@ModelAttribute("command") Bank bank,
			HttpServletRequest request)
	{
		Integer id=Integer.parseInt(request.getParameter("id"));
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("bank",bankService.get(id));
		return new ModelAndView("bank",model);
	}

	//delete Bank
	@RequestMapping("/user/deleteBank")
	public String deleteBank(HttpServletRequest request)
	{
		Integer id=Integer.parseInt(request.getParameter("id"));
		bankService.delete(id);
		return "redirect:/user/addBank.htm";
	}

	@RequestMapping(value="/common/bankDeposit",method=RequestMethod.GET)
	public String bankDeposit(@ModelAttribute("command") BankDeposit deposit,HttpSession session,ModelMap model) {
		model.put("banks", bankService.getBankName());
		if(session.getAttribute("role").equals("ROLE_ADMIN"))
			return "bankDepositAdmin";
		else
			return "bankDepositUser";
	}
	@RequestMapping(value = "/common/bankDeposit", method = RequestMethod.POST)
	public String addDeposit(@ModelAttribute("command") BankDeposit deposit) {
		Integer bankId=deposit.getBank().getBankId();
		bankService.addDeposit(bankId, deposit);
		return "redirect:/common/bankDeposit.htm";
	}
	@RequestMapping(value="/common/bankWithdrawl",method=RequestMethod.GET)
	public String bankWithdrawl(ModelMap model,HttpSession session) {
		model.put("banks", bankService.getBankName());
		if(session.getAttribute("role").equals("ROLE_ADMIN"))
			return "bankWithdrawlAdmin";
		else
			return "bankWithdrawlUser";
	}
	@RequestMapping(value = "/common/bankWithdrawl", method = RequestMethod.POST)
	public String addWithdraw(@ModelAttribute("command") BankWithdraw withdraw) {
		Integer bankId=withdraw.getBank().getBankId();
		bankService.addWithdraw(bankId, withdraw);
		return "redirect:/common/bankWithdrawl.htm";
	}
	@RequestMapping(value="/common/printCheck",method=RequestMethod.GET)
	public String printCheck(HttpSession session) {
		if(session.getAttribute("role").equals("ROLE_ADMIN"))
			return "printCheckAdmin";
		else
			return "printCheckUser";

	}

	@RequestMapping(value="/admin/memberDetailsCA",method=RequestMethod.GET)
	public String memberDetailsCA() {
		return "memberDetailsCA";
	}
	@RequestMapping(value="/admin/cancelDetailsCA",method=RequestMethod.GET)
	public String cancelDetailsCA() {
		return "cancelDetailsCA";
	}
	@RequestMapping(value="/admin/prizeDetailsCA",method=RequestMethod.GET)
	public String prizeDetailsCA() {
		return "prizeDetailsCA";
	}

}
